package controller

import (
	"fmt"
	"gitee.com/Frank098/container-management/model"
	"gitee.com/Frank098/container-management/service"
	"gitee.com/Frank098/container-management/service/ws/podws"
	"github.com/gin-gonic/gin"
	"golang.org/x/net/websocket"
	"io"
)

var Pod pod

type pod struct{}

// @Tags pods
// @Summary 查询pods详细信息
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "查询pods详细信息"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /pods/info [GET]
func (p *pod) GetPodInfo(ctx *gin.Context) {
	// 匿名结构体用于定义入参,get请求为form格式,其他为json格式
	///api/pods/info?pod_name=sleep-64d7d56698-t599r&namespace=legacy
	var params model.GetPod
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	data, err := service.Pod.GetPodInfo(params.PodName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags pods
// @Summary 删除pods
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "删除pods"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /pods/delete [DELETE]
func (p *pod) DeletePod(ctx *gin.Context) {
	//
	var params model.GetPod
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	err := service.Pod.DeletePod(params.PodName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "执行删除Pod成功",
	})
}

// @Tags pods
// @Summary 查询每个命名空间下pods信息
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "查询每个命名空间下pods信息"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /pods/all [GET]
func (p *pod) GetPodAllPerNp(ctx *gin.Context) {
	data, err := service.Pod.GetPodAllPerNp()
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags pods
// @Summary 查询单个命名空间下pods信息
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "查询单个命名空间下pods信息"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /pods/single [GET]
func (p *pod) GetPodSingleNp(ctx *gin.Context) {
	var params model.GetPodLog
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	data, err := service.Pod.GetPodSingleNp(params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags pods
// @Summary 在pods中执行命令
// @Accept json
// @Produce json
// @Param "" body model.PodCmdExec true "在pods中执行命令"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /pods/cmd [POST]
func (p *pod) PodCmdExec(ctx *gin.Context) {
	var params model.PodCmdExec
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	data, err := service.Pod.PodCmdExec(service.K8s.Config, params.PodName, params.Namespace, params.Cmd)
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags pods
// @Summary 获取pods编排文件
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "获取pods编排文件"
// @Success 200 {object} model.PodYamlResponse ""
// @Failure 400 {object} model.Result ""
// @Router /pods/yaml [GET]
func (p *pod) GetPodYaml(ctx *gin.Context) {
	var params model.GetPod
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	data, err := service.Pod.GetPodYaml(params.PodName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags pods
// @Summary 提供pods终端命令行操作
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "提供pods终端命令行操作"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /pods/ssh [GET]
func (p *pod) PodSsh(ctx *gin.Context) {
	podws.PodWsSsh(ctx.Writer, ctx.Request)
}

// @Tags pods
// @Summary 查询pods日志
// @Accept json
// @Produce json
// @Param "" body model.GetPod true "查询pods日志"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /pods/log [GET]
func (p *pod) PodLog(ctx *gin.Context) {
	var params model.GetPod
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	reader, err := podws.SteamLog(params.PodName, params.Namespace)
	if err != nil {
		return
	}
	websocket.Handler(func(ws *websocket.Conn) {
		defer ws.Close()
		defer reader.Close()
		//设置websocket payload为2 解决Could not decode a text frame as UTF-8
		//ws.PayloadType = websocket.BinaryFrame
		go io.Copy(ws, reader)
		buf := make([]byte, 256)
		for {
			_, err := ws.Read(buf)
			if err != nil {
				_, _ = ws.Write([]byte(err.Error()))
				break
			}
			fmt.Print(buf)
			_, _ = ws.Write(buf)
		}
	}).ServeHTTP(ctx.Writer, ctx.Request)
	return
}

/*
func (p *pod) PodLog(ctx *gin.Context) {
	podws.HandlerLog(ctx.Writer, ctx.Request)
}


func (p *pod) GetPodLog(ctx *gin.Context) {
	//127.0.0.1:8090/api/pods/log?pod_name=web-01-86d5b99ff9-wc2w6&namespace=default&container_name=web-01&tailLines=30&previous=false&follow=false
	var params model.GetPodLog
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err)
		return
	}
	data, err := service.Pod.GetPodLog(params.ContainerName, params.PodName, params.Namespace, params.Follow, params.TailLines, params.Previous)
	if err != nil {
		ErrorResponse(ctx, err)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

*/
